package com.yaozheng.vocationaltraining.manage;

import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.view.LoadMoreView;
import com.yaozheng.vocationaltraining.view.LoadMoreView_;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

/**
 * listView加载更多管理
 *
 * @author Administrator
 */
public class ListViewLoadMoreManage implements OnScrollListener {
    ILoadMoreView iLoadMoreView;
    ListView listView;
    LoadMoreView loadMoreView;
    boolean isLoadMore;

    public ListViewLoadMoreManage(ILoadMoreView iLoadMoreView,
                                  BaseActivity baseActivity, ListView listView) {
        super();
        this.listView = listView;
        this.iLoadMoreView = iLoadMoreView;
        loadMoreView = LoadMoreView_.build(baseActivity);
        listView.setFooterDividersEnabled(false);
        listView.addFooterView(loadMoreView);

        listView.setOnScrollListener(this);
        loadMoreComplete();

    }

    public boolean isLoadMore(int firstVisibleItem, int visibleItemCount,
                              int totalItemCount) {
        if ((firstVisibleItem + visibleItemCount) >= (totalItemCount - 1)) {
            return true;
        }
        return false;
    }

    /**
     * 加载更多
     *
     * @return
     */
    private boolean loadMore() {
        if (iLoadMoreView != null) {
            if (loadMoreView.getVisibility() == View.VISIBLE) {
                return false;
            } else {
                showLoadMoreView();
                iLoadMoreView.loadMoreData();
                return true;
            }
        }
        return false;
    }

    /**
     * 加载更多完成
     */
    public void loadMoreComplete() {
        isLoadMore = false;
        hideLoadMoreView();
    }

    /**
     * 显示加载更多
     */
    private void showLoadMoreView() {

        if (loadMoreView.getVisibility() != View.VISIBLE) {

            loadMoreView.setVisibility(View.VISIBLE);

            loadMoreView.setPadding(0, 0, 0, 0);
        }
    }

    /**
     * 隐藏加载更多
     */
    private void hideLoadMoreView() {
        if (loadMoreView.getVisibility() != View.GONE) {
            loadMoreView.setVisibility(View.GONE);
            loadMoreView.setPadding(0, -loadMoreView.getHeight(), 0, 0);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        isLoadMore = isLoadMore(firstVisibleItem, visibleItemCount,
                totalItemCount);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            // 当不滚动时
            case OnScrollListener.SCROLL_STATE_IDLE:

                // 判断滚动到底部
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                // System.out.println("滚动");
                System.out.println("停止");
                if (isLoadMore) {
                    isLoadMore = loadMore();
                }
                break;

            case OnScrollListener.SCROLL_STATE_FLING:
                System.out.println("FLING");
                break;
        }

    }

}
